<?php
/**
 * Created by PhpStorm.
 * User: zc
 * Date: 2019/8/5
 * Time: 23:41
 */
namespace app\common\logic;

use addons\adszone\library\Adszone;
use app\admin\controller\Zlog;
use think\Db;
use think\Exception;

class EvaluationM
{
    private $user_id = 0;
    private $org_cate_id = 0;
    private $id = 0;
    private $page = 1;
    private $pagesize = 15;

    /*
     * 获取测评列表方法
     * @return array
     */
    public function getList()
    {
        $data = Db::table('edu_evaluation')->field('*')
            ->order('buy_numbers', 'desc')
            ->paginate($this->pagesize, false, ['page'=>$this->page])->toArray();
        $pic = new Adszone();
        $images = $pic->getAdsByMark('ceping_img',ORG_CATE_ID);
        if(isset($data['data']) && !empty($data['data'])){
            foreach ($data['data'] as &$val){
                $val['createtime'] = date('Y-m-d', $val['createtime']);
                if(empty($val['image'])){
                    $val['image'] = isset($images['data'][0]['imageurl'])?$images['data'][0]['imageurl']:'';
                }

            }
        }
        return $data;
    }
    /*
     * 获取测评详情
     */
    public function getDetail()
    {
        if($this->id <= 0){
            return array();
        }
        $data = Db::name('evaluation')->field('*')->where('id', '=', $this->id)->find();
        $pic = new Adszone();
        $images = $pic->getAdsByMark('ceping_img',ORG_CATE_ID);
        if(empty($data['image'])){
            $data['image'] = isset($images['data'][0]['imageurl'])?$images['data'][0]['imageurl']:'';
        }
        $data['is_buy'] = $this->get_is_buy();
        if($data){
            $data['createtime'] = date('Y-m-d', $data['createtime']);
        }
        return $data;
    }
    public function get_is_buy() {

        $is_buy = Db::name('evaluation_order')->where([
            'user_id' => $this->user_id,
            'evalutation_id' => $this->id,
            'type' => 1
        ])->count();

        return $is_buy > 0 ? 1 : 0;  //1代表已经购买 0代表未购买
    }

    public function getOrder() {
        $order = Db::name('evaluation_order')->where([
            'user_id' => $this->user_id,
            'evalutation_id' => $this->id,
            'type' => 1
        ])->find();

        return $order;
    }

    //更新订单的 测评number
    public function setNumber($num) {
        Db::name('evaluation_order')->where([
            'user_id' => $this->user_id,
            'evalutation_id' => $this->id,
            'type' => 1
        ])->setField('line_number', $num);
    }
    /**
     * @param $user_id
     * @return $this
     */
    public function setUserId($user_id)
    {
        $this->user_id = $user_id;
        return $this;
    }

    /**
     * @return int
     */
    public function getUserId(){
        return $this->user_id;
    }
    /**
     * @param $org_cate_id
     * @return $this
     */
    public function setOrgCateId($org_cate_id)
    {
        $this->org_cate_id = $org_cate_id;
        return $this;
    }

    /**
     * @return int
     */
    public function getOrgCateId()
    {
        return $this->org_cate_id;
    }

    /**
     * @param $id
     * @return $this
     */
    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }

    /**
     * @return int
     */
    public function getId(){
        return $this->id;
    }

    /**
     * @param $page
     * @return $this
     */
    public function setPage($page)
    {
        $this->page = abs($page) > 0?abs($page):1;
        return $this;
    }

    /**
     * @return int
     */
    public function getPage(){
        return $this->page;
    }

    /**
     * @param $pagesize
     * @return $this
     */
    public function setPagesize($pagesize)
    {
        $this->pagesize = abs($pagesize) > 0?abs($pagesize):15;
        return $this;
    }

    /**
     * @return int
     */
    public function getPagesize(){
        return $this->pagesize;
    }


}